library ieee;
use ieee.std_logic_1164.all;
use IEEE.numeric_std.all;
use work.Achtung_const.all;

entity Game_Machine is
	port(
	clk						: in std_logic;
	clk_25					: in std_logic;
	reset					: in std_logic;
	start_P1				: in std_logic;
	start_P2				: in std_logic;
	init_x_P1, init_x_P2	: out unsigned(9 downto 0);
	init_y_P1, init_y_P2	: out unsigned(9 downto 0);
	init					: out std_logic;
	r,g,b					: out std_logic;
	videoOff				: in std_logic;
	R_SRAM, G_SRAM, B_SRAM	: in std_logic;
	X_VGA, Y_VGA			: in unsigned(9 downto 0);
	X_SRAM, Y_SRAM			: out unsigned(9 downto 0);
	SRAM_COMMAND			: out std_logic_vector(1 downto 0);
	completed				: in std_logic;
	P1_next_x				: in unsigned(9 downto 0);
	P1_next_y				: in unsigned (9 downto 0);
	out_pix					: out RAM_DATA
	);
end entity;

Architecture arch_1 of Game_Machine is
	type Game_State is (MENU, GAME_INIT, GAME_RUN, SAVE_P1, SAVE_P2, GAME_OVER);
	signal next_state, state	: Game_State;
	signal writing : unsigned(19 downto 0);
begin
	process(clk, state, start_P1, start_P2, videoOff, reset, R_SRAM, G_SRAM, B_SRAM, X_VGA, Y_VGA,
	P1_next_x, P1_next_y, completed,writing)
	variable index_var : integer ;
	begin
	index_var := 0;
		if(reset = '0') then
			state <= MENU;
			writing <= "00000000000000000000";
		else
			if(clk'event and clk = '1') then
				state <= next_state;
			end if;
			
			case state is
			when MENU =>
				r <= '0';
				g <= '0';
				b <= '1';
				
				init <= '0';
				if(start_P1 = '0') then
					next_state <= GAME_INIT;
				else
					next_state <= MENU;
				end if;
			when GAME_INIT =>
				r <= '0';
				g <= '1';
				b <= '0';
				SRAM_COMMAND <= RAM_WRIT_COMM;
				X_SRAM <= X_VGA;
				Y_SRAM <= Y_VGA;
				out_pix <= BACKGROUND;
				
					if(rising_edge(clk)) then	writing <= writing + "00000000000000000001";
				end if;
				
				init_x_P1 <= "0001000000";
				init_y_P1 <= "0001000000";
				init_x_P2 <= "0000110010";
				init_y_P2 <= "0110101101";
				init <= '1';
				if(writing > "11110100001001000000") then
					next_state <= GAME_RUN;
					else
					next_state <= GAME_INIT;
			end if;
				when GAME_RUN =>
				init <= '0';
				r <= R_SRAM;
				g <= G_SRAM;
				b <= B_SRAM;
				
				X_SRAM <= X_VGA;
				Y_SRAM <= Y_VGA;
				
				out_pix <= BACKGROUND;
				
				SRAM_COMMAND <= RAM_READ_COMM;
				
				if(videoOff = '1' ) then
					next_state <= SAVE_P1;
				else
					next_state <= GAME_RUN;
				end if;
				
			when SAVE_P1 =>
				r <= '0';
				g <= '0';
				b <= '0';
				init <= '0';
				X_SRAM <= P1_next_x;
				Y_SRAM <= P1_next_y;
				SRAM_COMMAND <= RAM_WRIT_COMM;
				
				out_pix <= PLAYER1;
				
				next_state <= GAME_RUN;
				
			when SAVE_P2 =>
				init <= '0';
				out_pix <= PLAYER2;
				if (completed = '1') then
					next_state <= GAME_RUN;
				else
					next_state <= SAVE_P2;
				end if;
			when GAME_OVER =>
				init <= '0';
				r <= '1';
				g <= '0';
				b <= '0';
				if(start_P1 = '0') then
					next_state <= MENU;
				else
					next_state <= GAME_OVER;
				end if;
		end case;
			
			
		end if;
	end process;
	
end arch_1;